from rest_framework import serializers
from .models import CrawlerTask, CrawledData

class CrawledDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = CrawledData
        fields = ('id', 'data', 'metadata', 'created_at')
        read_only_fields = ('id', 'created_at')

class CrawlerTaskSerializer(serializers.ModelSerializer):
    crawled_data_count = serializers.SerializerMethodField()

    class Meta:
        model = CrawlerTask
        fields = ('id', 'name', 'data_source', 'status', 'parameters', 
                 'progress', 'error_message', 'total_items', 'crawled_items',
                 'created_at', 'updated_at', 'completed_at', 'crawled_data_count')
        read_only_fields = ('id', 'status', 'progress', 'error_message',
                          'total_items', 'crawled_items', 'created_at', 
                          'updated_at', 'completed_at', 'crawled_data_count')

    def get_crawled_data_count(self, obj):
        return obj.crawled_data.count()

class CrawlerTaskCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = CrawlerTask
        fields = ('name', 'data_source', 'parameters')

class CrawlerTaskUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = CrawlerTask
        fields = ('status', 'progress', 'error_message', 'total_items', 
                 'crawled_items', 'completed_at')
        read_only_fields = ('completed_at',) 